package 顺序表.基础;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

public class Traversal {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(5);
        list.add(9);
        list.add(18);

        //遍历1
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i)+" ");
        }
        System.out.println();
        //遍历2
        for (Integer x:list) {
            System.out.println(x);
        }
        System.out.println();
        //遍历3
        Iterator<Integer> iterator = list.iterator();
        while(iterator.hasNext()){//是否存在下一个
            System.out.println(iterator.next()+" ");
        }
        System.out.println();


//      ListIterator继承Iterator
        ListIterator listIterator = list.listIterator();//迭代器
        while(listIterator.hasNext()){
            System.out.println(listIterator.next()+" ");
        }
        System.out.println();

        //从后往前
        ListIterator listIterator1 =list.listIterator(list.size());
        while(listIterator1.hasPrevious()){
            System.out.println(listIterator1.previous()+" ");
        }
    }
}
